home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htsname.c < prev    next >
C/C++ Source or Header  |  2007-06-15  |  52KB  |  1,487 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       savename routine (compute output filename)             */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htscore.h"
  42. #include "htsname.h"
  43. #include "htsmd5.h"
  44. #include "htstools.h"
  45. #include <ctype.h>
  46.  
  47. #undef test_flush
  48. #define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); }
  49.  
  50. #define ADD_STANDARD_PATH \
  51.     {  /* ajout nom */\
  52.       char BIGSTK buff[HTS_URLMAXSIZE*2];\
  53.       buff[0]='\0';\
  54.       strncatbuff(buff,start_pos,(int) (nom_pos - start_pos));\
  55.       url_savename_addstr(save,buff);\
  56.     }
  57.  
  58. #define ADD_STANDARD_NAME(shortname) \
  59.     {  /* ajout nom */\
  60.       char BIGSTK buff[HTS_URLMAXSIZE*2];\
  61.       standard_name(buff,dot_pos,nom_pos,fil_complete,(shortname));\
  62.       url_savename_addstr(save,buff);\
  63.     }
  64.  
  65.  
  66. /* Avoid stupid DOS system folders/file such as 'nul' */
  67. /* Based on linux/fs/umsdos/mangle.c */
  68. static const char *hts_tbdev[] =
  69. {
  70.     "/prn", "/con", "/aux", "/nul",
  71.     "/lpt1", "/lpt2", "/lpt3", "/lpt4",
  72.     "/com1", "/com2", "/com3", "/com4",
  73.     "/clock$",
  74.     "/emmxxxx0", "/xmsxxxx0", "/setverxx",
  75.     ""
  76. };
  77.  
  78.  
  79. #define URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET() do { \
  80.   int prev = opt->state._hts_in_html_parsing; \
  81.   while(back_pluggable_sockets_strict(sback, opt) <= 0) { \
  82.     opt->state. _hts_in_html_parsing = 6; \
  83.     /* Wait .. */ \
  84.     back_wait(sback,opt,cache,0); \
  85.     /* Transfer rate */ \
  86.     engine_stats(); \
  87.     /* Refresh various stats */ \
  88.     HTS_STAT.stat_nsocket=back_nsoc(sback); \
  89.     HTS_STAT.stat_errors=fspc(opt,NULL,"error"); \
  90.     HTS_STAT.stat_warnings=fspc(opt,NULL,"warning"); \
  91.     HTS_STAT.stat_infos=fspc(opt,NULL,"info"); \
  92.     HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr); \
  93.     HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback); \
  94.     /* Check */ \
  95.     { \
  96.       if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count,-1,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) { \
  97.         return -1; \
  98.       } \
  99.     } \
  100.   } \
  101.   opt->state._hts_in_html_parsing = prev; \
  102. } while(0)
  103.  
  104.  
  105. // forme le nom du fichier α sauver (save) α partir de fil et adr
  106. // systΦme intelligent, qui renomme en cas de besoin (exemple: deux INDEX.HTML et index.html)
  107. int url_savename(char* adr_complete, char* fil_complete, char* save, 
  108.                                  char* former_adr, char* former_fil, 
  109.                                  char* referer_adr, char* referer_fil, 
  110.                                  httrackp* opt, 
  111.                                  lien_url** liens, int lien_tot, 
  112.                                  struct_back* sback, cache_back* cache, hash_struct* hash, 
  113.                                  int ptr, int numero_passe, const lien_back* headers) {
  114.     char catbuff[CATBUFF_SIZE];
  115.   const char* mime_type = ( headers && HTTP_IS_OK(headers->r.statuscode) ) ? headers->r.contenttype : NULL;
  116.   lien_back* const back = sback->lnk;
  117.   /* */
  118.   char BIGSTK newfil[HTS_URLMAXSIZE*2];   /* ="" */
  119.   /*char BIGSTK normadr_[HTS_URLMAXSIZE*2];*/
  120.   char BIGSTK normadr_[HTS_URLMAXSIZE*2], normfil_[HTS_URLMAXSIZE*2];
  121.     enum { PROTOCOL_HTTP, PROTOCOL_HTTPS, PROTOCOL_FTP, PROTOCOL_FILE, PROTOCOL_MMS, PROTOCOL_UNKNOWN };
  122.   static const char* protocol_str[] = {"http", "https", "ftp", "file", "mms", "unknown"};
  123.   int protocol = PROTOCOL_HTTP;
  124.   char* normadr;
  125.   char* normfil;
  126.   char* fil;
  127.   char* adr;
  128.   char* print_adr;
  129.   char *start_pos=NULL,*nom_pos=NULL,*dot_pos=NULL;  // Position nom et point
  130.   // pour changement d'extension ou de nom (content-disposition)
  131.   int ext_chg=0, ext_chg_delayed=0;
  132.   int is_html=0;
  133.   char ext[256];
  134.   int max_char=0;
  135.   //CLEAR
  136.   newfil[0]=ext[0]='\0';
  137.  
  138.   /* 8-3 ? */
  139.   switch(opt->savename_83) {
  140.   case 1:     // 8-3
  141.     max_char=8;
  142.     break;
  143.   case 2:     // Level 2 File names may be up to 31 characters.
  144.     max_char=31;
  145.     break;
  146.   default:
  147.     max_char=8;
  148.     break;
  149.   }
  150.  
  151.   // effacer save
  152.   save[0]='\0';
  153.   // fil
  154.   fil = fil_complete;
  155.   // copy of fil, used for lookups (see urlhack)
  156.   normfil = fil;
  157.   // et adr (sauter user/pass)
  158.   // on prend le parti de mettre les fichiers avec login/pass au mΩme endroit que si ils
  159.   // Θtaient capturΘs sans ces paramΦtres
  160.   // c'est pour cette raison qu'on ignore totalement adr_complete (mΩme pour la recherche en table de hachage)
  161.   adr = jump_identification(adr_complete);
  162.   // copy of adr, used for lookups (see urlhack)
  163.   normadr = adr;
  164.  
  165.   // normalize the URL:
  166.   // www.foo.com -> foo.com
  167.   // www-42.foo.com -> foo.com
  168.   // foo.com/bar//foobar -> foo.com/bar/foobar
  169.   if (opt->urlhack) {
  170.     // copy of adr (without protocol), used for lookups (see urlhack)
  171.     normadr=adr_normalized(adr, normadr_);
  172.     normfil=fil_normalized(fil,normfil_);
  173.   } else {
  174.     if (link_has_authority(adr_complete)) {     // https or other protocols : in "http/" subfolder
  175.       char* pos = strchr(adr_complete, ':');
  176.       if (pos != NULL) {
  177.         normadr_[0] = '\0';
  178.         strncatbuff(normadr_, adr_complete, (int)(pos - adr_complete));
  179.         strcatbuff(normadr_, "://");
  180.         strcatbuff(normadr_, normadr);
  181.         normadr=normadr_;
  182.       }
  183.     }
  184.   }
  185.  
  186.   // α afficher sans ftp://
  187.   print_adr=jump_protocol(adr);
  188.   if (strfield(adr_complete, "https:")) {
  189.       protocol = PROTOCOL_HTTPS;
  190.   } else if (strfield(adr_complete, "ftp:")) {
  191.       protocol = PROTOCOL_FTP;
  192.   } else if (strfield(adr_complete, "file:")) {
  193.       protocol = PROTOCOL_FILE;
  194.   } else if (strfield(adr_complete, "mms:")) {
  195.       protocol = PROTOCOL_MMS;
  196.   } else {
  197.       protocol = PROTOCOL_HTTP;
  198.   }
  199.  
  200.   // court-circuit pour lien primaire
  201.   if (strnotempty(adr)==0) {
  202.     if (strcmp(fil,"primary")==0) {
  203.       strcatbuff(save,"primary.html");
  204.       return 0;
  205.     }
  206.   }
  207.  
  208.  
  209.   // vΘrifier que le nom n'a pas dΘja ΘtΘ calculΘ (si oui le renvoyer tel que)
  210.   // vΘrifier que le nom n'est pas dΘja pris...
  211.   // NOTE: si on cherche /toto/ et que /toto est trouvΘ on le prend (et rΘciproquqment) ** // **
  212.   if (liens!=NULL) { 
  213.     int i;
  214.  
  215.     i=hash_read(hash,normadr,normfil,1,opt->urlhack);      // recherche table 1 (adr+fil)
  216.     if (i>=0) {    // ok, trouvΘ
  217.       strcpybuff(save,liens[i]->sav);
  218.       return 0;
  219.     }
  220.     i=hash_read(hash,normadr,normfil,2,opt->urlhack);      // recherche table 2 (former_adr+former_fil)
  221.     if (i>=0) {    // ok, trouvΘ
  222.       // copier location moved!
  223.       strcpybuff(adr_complete,liens[i]->adr);
  224.       strcpybuff(fil_complete,liens[i]->fil);
  225.       // et save
  226.       strcpybuff(save,liens[i]->sav);  // copier (formΘ α partir du nouveau lien!)
  227.       return 0;
  228.     }
  229.  
  230.     // chercher sans / ou avec / dans former
  231.     {
  232.       char BIGSTK fil_complete_patche[HTS_URLMAXSIZE*2];
  233.       strcpybuff(fil_complete_patche,normfil);
  234.       // Version avec ou sans /
  235.       if (fil_complete_patche[strlen(fil_complete_patche)-1]=='/')
  236.         fil_complete_patche[strlen(fil_complete_patche)-1]='\0';
  237.       else
  238.         strcatbuff(fil_complete_patche,"/");
  239.       i=hash_read(hash,normadr,fil_complete_patche,2,opt->urlhack);      // recherche table 2 (former_adr+former_fil)
  240.       if (i>=0) {
  241.         // Θcraser fil et adr (pas former_fil?????)
  242.         strcpybuff(adr_complete,liens[i]->adr);
  243.         strcpybuff(fil_complete,liens[i]->fil);
  244.         // Θcrire save
  245.         strcpybuff(save,liens[i]->sav);
  246.         return 0;
  247.       }
  248.     }
  249.   }
  250.  
  251.   // vΘrifier la non prΘsence de paramΦtres dans le nom de fichier
  252.   // si il y en a, les supprimer (ex: truc.cgi?subj=aspirateur)
  253.   // nΘanmoins, gardΘ pour vΘrifier la non duplication (voir aprΦs)
  254.   {
  255.     char* a;
  256.     a=strchr(fil,'?');
  257.     if (a!=NULL) {
  258.       strncatbuff(newfil,fil,(int) (a - fil));
  259.     } else {
  260.       strcpybuff(newfil,fil);
  261.     }
  262.     fil=newfil;
  263.   }
  264.   // Decode remaining %
  265.   strcpybuff(fil,unescape_http(catbuff,fil));
  266.     // , BUT do not decode high chars
  267.   //strcpybuff(fil,unescape_http_unharm(fil, 1));
  268.     // YES (not server side, but fs/client side)
  269.  
  270. #if HTS_USEMMS
  271.     /* .asx hack */
  272.     if (headers != NULL && headers->r.cdispo[0] != 0 
  273.         && strfield(headers->r.contenttype, "video/")
  274.         && strfield2(get_ext(OPT_GET_BUFF(opt),headers->r.cdispo), "asx") == 0) 
  275.     {
  276.         ext_chg = 1;
  277.         strcpybuff(ext, "asx");
  278.     }
  279.     else if (headers != NULL && headers->r.contenttype[0] != 0 
  280.         && strfield2(headers->r.contenttype, "video/x-ms-asf"))
  281.     {
  282.         char *exts = get_ext(OPT_GET_BUFF(opt),headers->url_fil);
  283.         if (strfield2(exts, "wmv") == 0)
  284.         {
  285.             ext_chg = 1;
  286.             strcpybuff(ext, "wmv");
  287.         }
  288.         else if (strfield2(exts, "asf") == 0)
  289.         {
  290.             ext_chg = 1;
  291.             strcpybuff(ext, "asf");
  292.         }
  293.         else if (strfield2(exts, "avi") == 0)
  294.         {
  295.             ext_chg = 1;
  296.             strcpybuff(ext, "avi");
  297.         }
  298.         else if (strfield2(exts, "asx") == 0)
  299.         {
  300.             ext_chg = 1;
  301.             strcpybuff(ext, "asx");
  302.         }
  303.     }
  304. #endif
  305.  
  306.   /* replace shtml to html.. */
  307.   if (opt->savename_delayed == 2)
  308.     is_html = -1;            /* ALWAYS delay type */
  309.   else
  310.     is_html = ishtml(opt,fil);
  311.   switch ( is_html ) {       /* .html,.shtml,.. */
  312.   case 1:
  313.     if ( 
  314.       (strfield2(get_ext(OPT_GET_BUFF(opt),fil),"html") == 0)
  315.       && (strfield2(get_ext(OPT_GET_BUFF(opt),fil),"htm") == 0)
  316.       )
  317.     {
  318.       strcpybuff(ext,"html");
  319.       ext_chg=1;
  320.     }
  321.     break;
  322.   case 0:
  323.     if (!strnotempty(ext)) {
  324.       if (is_userknowntype(opt,fil)) {      // mime known by user
  325.         char BIGSTK mime[1024];
  326.         mime[0]=ext[0]='\0';
  327.         get_userhttptype(opt,mime,fil);
  328.         if (strnotempty(mime)) {
  329.           give_mimext(ext,mime);
  330.           if (strnotempty(ext)) {
  331.             ext_chg=1;
  332.           }
  333.         }
  334.       }
  335.     }
  336.     break;
  337.   }
  338.  
  339.   // si option check_type activΘe
  340.   if (is_html < 0 && opt->check_type && !ext_chg) {
  341.     int ishtest=0;
  342.     if ( (!strfield(adr_complete,"file://")) 
  343.       && (!strfield(adr_complete,"ftp://")) 
  344. #if HTS_USEMMS
  345.       && (!strfield(adr_complete,"mms://")) 
  346. #endif
  347.       ) {
  348.       // tester type avec requΦte HEAD si on ne connait pas le type du fichier
  349.       if (!(   (opt->check_type==1) && (fil[strlen(fil)-1]=='/')   ))    // slash doit Ωtre html?
  350.       if ( opt->savename_delayed == 2 || (ishtest=ishtml(opt,fil)) < 0) { // on ne sait pas si c'est un html ou un fichier..
  351.         // lire dans le cache
  352.         htsblk r = cache_read(opt,cache,adr,fil,NULL,NULL);              // test uniquement
  353.         if (r.statuscode != -1) {  // pas d'erreur de lecture cache
  354.           char s[16]; s[0]='\0';
  355.           if ( (opt->debug>1) && (opt->log!=NULL) ) {
  356.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link type (from cache) %s%s"LF,adr_complete,fil_complete);
  357.             test_flush;
  358.           }
  359.           if (strnotempty(r.cdispo)) {        /* filename given */
  360.             ext_chg=2;      /* change filename */
  361.             strcpybuff(ext,r.cdispo);
  362.           }
  363.           else if (!may_unknown(opt,r.contenttype) || ishtest == -2) {  // on peut patcher α priori?
  364.             give_mimext(s,r.contenttype);  // obtenir extension
  365.             if (strnotempty(s)>0) {        // on a reconnu l'extension
  366.               ext_chg=1;
  367.               strcpybuff(ext,s);
  368.             }
  369.           }
  370.           //
  371.         } else if ( opt->savename_delayed != 2 && is_userknowntype(opt,fil)) {   /* PATCH BY BRIAN SCHR╓DER. 
  372.                                                                             Lookup mimetype not only by extension, 
  373.                                                                             but also by filename */
  374.           /* Note: "foo.cgi => text/html" means that foo.cgi shall have the text/html MIME file type,
  375.           that is, ".html" */
  376.           char BIGSTK mime[1024];
  377.           mime[0]=ext[0]='\0';
  378.           get_userhttptype(opt, mime, fil);
  379.           if (strnotempty(mime)) {
  380.             give_mimext(ext, mime);
  381.             if (strnotempty(ext)) {
  382.               ext_chg=1;
  383.             }
  384.           }
  385.         }
  386.         // note: if savename_delayed is enabled, the naming will be temporary (and slightly invalid!)
  387.         else if (opt->savename_delayed != 0) {
  388.           if (mime_type != NULL)  {
  389.             ext[0] = '\0';
  390.             if (*mime_type) {
  391.               give_mimext(ext, mime_type);
  392.             }
  393.             if (strnotempty(ext)) {
  394.               char mime_from_file[128];
  395.               mime_from_file[0] = 0;
  396.               get_httptype(opt, mime_from_file, fil, 1);
  397.               if (!strnotempty(mime_from_file) || strcasecmp(mime_type, mime_from_file) != 0) {    /* different mime for this type */
  398.                 if (!may_unknown(opt, mime_type)) {
  399.                   ext_chg = 1;
  400.                 }
  401.               } else {
  402.                 ext_chg = 0;
  403.               }
  404.             }
  405.           } else {
  406.             /* Avoid collisions (no collisionning detection) */
  407.             sprintf(ext, "%x.%s", opt->state.delayedId++, DELAYED_EXT);
  408.             ext_chg = 1;
  409.                         ext_chg_delayed = 1;        /* due to naming system */
  410.           }
  411.         }
  412.         // test imposible dans le cache, faire une requΩte
  413.         else {
  414.           //
  415.           int hihp = opt->state._hts_in_html_parsing;
  416.           int has_been_moved=0;
  417.           char BIGSTK curr_adr[HTS_URLMAXSIZE*2],curr_fil[HTS_URLMAXSIZE*2];
  418.           
  419.           /* Ensure we don't use too many sockets by using a "testing" one
  420.              If we have only 1 simultaneous connection authorized, wait for pending download
  421.              Wait for an available slot 
  422.           */
  423.           URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET();
  424.  
  425.           /* Rock'in */
  426.           curr_adr[0]=curr_fil[0]='\0';
  427.          opt->state. _hts_in_html_parsing=2;  // test
  428.           if ( (opt->debug>1) && (opt->log!=NULL) ) {
  429.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link type %s%s"LF,adr_complete,fil_complete);
  430.             test_flush;
  431.           }
  432.           strcpybuff(curr_adr,adr_complete);
  433.           strcpybuff(curr_fil,fil_complete);
  434.           // ajouter dans le backing le fichier en mode test
  435.           // savename: rien car en mode test
  436.           if (back_add(sback,opt,cache,curr_adr,curr_fil,BACK_ADD_TEST,referer_adr,referer_fil,1)!=-1) {
  437.             int b;
  438.             b=back_index(opt,sback,curr_adr,curr_fil,BACK_ADD_TEST);         
  439.             if (b>=0) {
  440.               int stop_looping=0;
  441.               int petits_tours=0;
  442.               int get_test_request=0;       // en cas de bouclage sur soi mΩme avec HEAD, tester avec GET.. parfois c'est la cause des problΦmes
  443.               do {
  444.                 // temps α attendre, et remplir autant que l'on peut le cache (backing)
  445.                 if (back[b].status>0) {
  446.                   back_wait(sback,opt,cache,0);        
  447.                 }
  448.                 if (ptr>=0) {
  449.                                     back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  450.                                 }
  451.  
  452.                                 // on est obligΘ d'appeler le shell pour le refresh..
  453.                                 // Transfer rate
  454.                                 engine_stats();
  455.  
  456.                                 // Refresh various stats
  457.                                 HTS_STAT.stat_nsocket=back_nsoc(sback);
  458.                                 HTS_STAT.stat_errors=fspc(opt,NULL,"error");
  459.                                 HTS_STAT.stat_warnings=fspc(opt,NULL,"warning");
  460.                                 HTS_STAT.stat_infos=fspc(opt,NULL,"info");
  461.                                 HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  462.                                 HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  463.  
  464.                 if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count,b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  465.                   return -1;
  466.                 } else if (opt->state._hts_cancel || !back_checkmirror(opt)) {    // cancel 2 ou 1 (cancel parsing)
  467.                   back_delete(opt,cache,sback,b);       // cancel test
  468.                   stop_looping = 1;
  469.                 }
  470.  
  471.                 // traitement des 304,303..
  472.                 if (back[b].status<=0) {
  473.                   if (HTTP_IS_REDIRECT(back[b].r.statuscode)) {    // agh moved.. un tit tour de plus
  474.                     if ((petits_tours<5) && (former_adr) && (former_fil)) { // on va pas tourner en rond non plus!
  475.                       if ((int) strnotempty(back[b].r.location)) {    // location existe!
  476.                         char BIGSTK mov_url[HTS_URLMAXSIZE*2],mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  477.                         mov_url[0]=mov_adr[0]=mov_fil[0]='\0';
  478.                         //
  479.                         strcpybuff(mov_url,back[b].r.location);    // copier URL
  480.                         if (ident_url_relatif(mov_url,curr_adr,curr_fil,mov_adr,mov_fil)>=0) {                        
  481.                           // si non bouclage sur soi mΩme, ou si test avec GET non testΘ
  482.                           if ((strcmp(mov_adr,curr_adr)) || (strcmp(mov_fil,curr_fil)) || (get_test_request==0)) {
  483.                             // bouclage?
  484.                             if ((!strcmp(mov_adr,curr_adr)) && (!strcmp(mov_fil,curr_fil)))
  485.                               get_test_request=1;     // faire requΦte avec GET
  486.  
  487.                             // recopier former_adr/fil?
  488.                             if ((former_adr) && (former_fil)) {
  489.                               if (strnotempty(former_adr)==0) {    // Pas dΘja notΘ
  490.                                 strcpybuff(former_adr,curr_adr);
  491.                                 strcpybuff(former_fil,curr_fil);
  492.                               }
  493.                             }
  494.  
  495.                             // check explicit forbidden - don't follow 3xx in this case
  496.                             {
  497.                               int set_prio_to=0;
  498.                               if (hts_acceptlink(opt,ptr,lien_tot,liens,
  499.                                 mov_adr,mov_fil,
  500.                                 NULL, NULL,
  501.                                 &set_prio_to,
  502.                                 NULL) == 1) 
  503.                               {  /* forbidden */
  504.                                 has_been_moved = 1;
  505.                                 back_maydelete(opt,cache,sback,b);    // ok
  506.                                 strcpybuff(curr_adr,mov_adr);
  507.                                 strcpybuff(curr_fil,mov_fil);
  508.                                 mov_url[0]='\0';
  509.                                 stop_looping = 1;
  510.                               }
  511.                             }
  512.                             
  513.                             // ftp: stop!
  514.                             if (strfield(mov_url,"ftp://")
  515. #if HTS_USEMMS
  516.                                                             || strfield(mov_url,"mms://")
  517. #endif
  518.                                                             ) 
  519.                                                         {    // ftp, ok on arrΩte
  520.                               has_been_moved = 1;
  521.                               back_maydelete(opt,cache,sback,b);    // ok
  522.                               strcpybuff(curr_adr,mov_adr);
  523.                               strcpybuff(curr_fil,mov_fil);
  524.                               stop_looping = 1;
  525.                             } else if (*mov_url) {
  526.                               char* methode;
  527.                               if (!get_test_request)
  528.                                 methode=BACK_ADD_TEST;      // tester avec HEAD
  529.                               else {
  530.                                 methode=BACK_ADD_TEST2;     // tester avec GET
  531.                                 if ( opt->log!=NULL ) {
  532.                                   HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Loop with HEAD request (during prefetch) at %s%s"LF,curr_adr,curr_fil);
  533.                                   test_flush;
  534.                                 }                    
  535.                               }
  536.                               // Ajouter
  537.                               URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET();
  538.                               if (back_add(sback,opt,cache,mov_adr,mov_fil,methode,referer_adr,referer_fil,1)!=-1) {    // OK
  539.                                 if ( (opt->debug>1) && (opt->log!=NULL) ) {
  540.                                   HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"(during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil);
  541.                                   test_flush;
  542.                                 }
  543.                                 
  544.                                 // libΘrer emplacement backing actuel et attendre le prochain
  545.                                 back_maydelete(opt,cache,sback,b);
  546.                                 strcpybuff(curr_adr,mov_adr);
  547.                                 strcpybuff(curr_fil,mov_fil);
  548.                                 b=back_index(opt,sback,curr_adr,curr_fil,methode);         
  549.                                 if (!get_test_request)
  550.                                   has_been_moved = 1;       // sinon ne pas forcer has_been_moved car non dΘplacΘ
  551.                                 petits_tours++;
  552.                                 //
  553.                               } else {// sinon on fait rien et on s'en va.. (ftp etc)
  554.                                 if ( (opt->debug>1)  && (opt->log)) {
  555.                                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Warning: Savename redirect backing error at %s%s"LF,mov_adr,mov_fil);
  556.                                   test_flush;
  557.                                 } 
  558.                               }
  559.                             }
  560.                           } else {
  561.                             if ( opt->log!=NULL ) {
  562.                               HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop to same filename)"LF,adr_complete,fil_complete);
  563.                               test_flush;
  564.                             }
  565.                           }
  566.                           
  567.                         }
  568.                       }
  569.                     } else{  // arrΩter les frais
  570.                       if ( opt->log!=NULL ) {
  571.                         HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop)"LF,adr_complete,fil_complete);
  572.                         test_flush;
  573.                       }
  574.                     }
  575.                   }  // ok, leaving
  576.                 }
  577.               } while(!stop_looping && back[b].status > 0 && back[b].status < 1000);
  578.               
  579.               // Si non dΘplacΘ, forcer type?
  580.               if (!has_been_moved) {
  581.                 if (back[b].r.statuscode!=-10) {    // erreur
  582.                   if (strnotempty(back[b].r.contenttype)==0)
  583.                     strcpybuff(back[b].r.contenttype,"text/html");    // message d'erreur en html
  584.                   // Finalement on, renvoie un erreur, pour ne toucher α rien dans le code
  585.                   // libΘrer emplacement backing
  586.                   /*if (opt->log!=NULL) {
  587.                     fspc(opt->log,0); fprintf(opt->log,"Error: (during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil);
  588.                     test_flush;
  589.                   }                    
  590.                   back_delete(opt,cache,sback,b);
  591.                   return -1;        // ERREUR (404 par exemple)
  592.                   */
  593.                 } 
  594.                 
  595.                 {            // pas d'erreur, changer type?
  596.                   char s[16];
  597.                   s[0]='\0';
  598.                   if (strnotempty(back[b].r.cdispo)) {        /* filename given */
  599.                     ext_chg=2;      /* change filename */
  600.                     strcpybuff(ext,back[b].r.cdispo);
  601.                   }
  602.                   else if (!may_unknown(opt,back[b].r.contenttype) || ishtest == -2 ) {  // on peut patcher α priori? (pas interdit ou pas de type)
  603.                     give_mimext(s,back[b].r.contenttype);  // obtenir extension
  604.                     if (strnotempty(s)>0) {    // on a reconnu l'extension
  605.                       ext_chg=1;
  606.                       strcpybuff(ext,s);
  607.                     }
  608.                   }
  609.                 }
  610.               }
  611.               // FIN Si non dΘplacΘ, forcer type?
  612.  
  613.               // libΘrer emplacement backing
  614.               back_maydelete(opt,cache,sback,b);
  615.               
  616.               // --- --- ---
  617.               // oops, a ΘtΘ dΘplacΘ.. on recalcule en rΘcursif (osons!)
  618.               if (has_been_moved) {
  619.                 // copier adr, fil (optionnel, mais sinon marche pas pour le rip)
  620.                 strcpybuff(adr_complete,curr_adr);
  621.                 strcpybuff(fil_complete,curr_fil);
  622.                 // copier adr, fil
  623.                 
  624.                 return url_savename(curr_adr,curr_fil,save,NULL,NULL,referer_adr,referer_fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL);
  625.               }
  626.               // --- --- ---
  627.               
  628.             }
  629.             
  630.           } else {
  631.             printf("PANIC! : Savename Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  632. #if BDEBUG==1
  633.             printf("error while savename crash adding\n");
  634. #endif
  635.             if (opt->log) {
  636.               HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unexpected savename backing error at %s%s"LF,adr,fil_complete);
  637.               test_flush;
  638.             } 
  639.             
  640.           }
  641.           // restaurer
  642.           opt->state._hts_in_html_parsing=hihp;
  643.         }  // cachΘ?
  644.       }
  645.     }
  646.   }
  647.  
  648.  
  649.  
  650.   // - - - DEBUT NOMMAGE - - -
  651.  
  652.   // Donner nom par dΘfaut?
  653.   if (fil[strlen(fil)-1]=='/')  {
  654.     if (!strfield(adr_complete,"ftp://")
  655. #if HTS_USEMMS
  656.             && !strfield(adr_complete,"mms://")
  657. #endif
  658.             )
  659.         {
  660.       strcatbuff(fil,DEFAULT_HTML);     // nommer page par dΘfaut!!
  661. #if HTS_USEMMS
  662.         } else if (strfield(adr_complete,"mms://")) {
  663.             strcatbuff(fil,DEFAULT_MMS);
  664. #endif
  665.         } else {
  666.       if (!opt->proxy.active)
  667.         strcatbuff(fil,DEFAULT_FTP);     // nommer page par dΘfaut (texte)
  668.       else
  669.         strcatbuff(fil,DEFAULT_HTML);     // nommer page par dΘfaut (α priori ici html depuis un proxy http)
  670.     }
  671.   }
  672.   // Changer extension?
  673.   // par exemple, php3 sera sauvΘ en html, cgi en html ou gif, xbm etc.. selon les cas
  674.   if (ext_chg) {  // changer ext
  675.     char* a=fil+strlen(fil)-1;
  676.     if ( (opt->debug>1) && (opt->log!=NULL) ) {
  677.       HTS_LOG(opt,LOG_DEBUG);
  678.       if (ext_chg==1)
  679.         fprintf(opt->log,"Changing link extension %s%s to .%s"LF,adr_complete,fil_complete,ext);
  680.       else
  681.         fprintf(opt->log,"Changing link name %s%s to %s"LF,adr_complete,fil_complete,ext);
  682.       test_flush;
  683.     }
  684.     if (ext_chg==1) {
  685.       while((a > fil) && (*a!='.') && (*a!='/')) a--;
  686.       if (*a=='.') *a='\0';  // couper
  687.       strcatbuff(fil,".");      // recopier point
  688.     } else {
  689.       while(( a > fil) && (*a!='/')) a--;
  690.       if (*a=='/') a++;
  691.       *a='\0';
  692.    }
  693.     strcatbuff(fil,ext);    // copier ext/nom
  694.   }
  695.  
  696.   // Rechercher premier / et dernier .
  697.   {  
  698.     char* a=fil+strlen(fil)-1;
  699.  
  700.     // passer structures
  701.     start_pos=fil;
  702.     while(( a > fil) && (*a != '/') && (*a != '\\')) {
  703.       if (*a == '.')    // point? noter position
  704.         if (!dot_pos)
  705.           dot_pos=a;
  706.         a--;
  707.     }
  708.     if ((*a=='/') || (*a=='\\')) a++;
  709.     nom_pos = a;
  710.   }
  711.  
  712.   
  713.   // un nom de fichier est gΘnΘrΘ
  714.   // s'il existe dΘja, alors on le mofifie lΘgΦrement
  715.  
  716.   // ajouter nom du site Θventuellement en premier
  717.   if (opt->savename_type == -1) {    // utiliser savename_userdef! (%h%p/%n%q.%t)
  718.     const char* a = StringBuff(opt->savename_userdef);
  719.     char* b = save;
  720.     /*char *nom_pos=NULL,*dot_pos=NULL;  // Position nom et point */
  721.     char tok;
  722.  
  723.     /*
  724.     {  // Rechercher premier /
  725.       char* a=fil+strlen(fil)-1;
  726.       // passer structures
  727.       while(((int) a>(int) fil) && (*a != '/') && (*a != '\\')) {
  728.         if (*a == '.')    // point? noter position
  729.         if (!dot_pos)
  730.           dot_pos=a;
  731.         a--;
  732.       }
  733.       if ((*a=='/') || (*a=='\\')) a++;
  734.       nom_pos = a;
  735.     }
  736.     */
  737.  
  738.     // Construire nom
  739.     while ((*a) && (((int) (b - save)) < HTS_URLMAXSIZE ) ) {    // parser, et pas trop long..
  740.       if (*a == '%') {
  741.         int short_ver=0;
  742.         a++;
  743.         if (*a == 's') {
  744.           short_ver=1;
  745.           a++;
  746.         }
  747.         *b='\0';
  748.         switch(tok=*a++) {
  749.           case '[':            // %[param:prefix_if_not_empty:suffix_if_not_empty:empty_replacement:notfound_replacement]
  750.             if (strchr(a,']')) {
  751.               int pos=0;
  752.               char name[5][256];
  753.               char* c=name[0];
  754.               for(pos = 0 ; pos < 5 ; pos++) {
  755.                 name[pos][0]='\0';
  756.               }
  757.               pos=0;
  758.               while(*a!=']') {
  759.                 if (pos < 5) {
  760.                   if (*a == ':') {  // next token
  761.                     c=name[++pos];
  762.                     a++;
  763.                   } else {
  764.                     *c++=*a++;
  765.                     *c='\0';
  766.                   }
  767.                 }
  768.               }
  769.               a++;
  770.               strcatbuff(name[0],"=");           /* param=.. */
  771.               c=strchr(fil_complete,'?');
  772.               /* parameters exists */
  773.               if (c) {
  774.                 char* cp;
  775.                 while((cp = strstr(c+1, name[0])) && *(cp-1) != '?' && *(cp-1) != '&') {     /* finds [?&]param= */
  776.                   c = cp;
  777.                 }
  778.                 if (cp) {
  779.                   c = cp + strlen(name[0]);    /* jumps "param=" */
  780.                   strcpybuff(b, name[1]);    /* prefix */
  781.                   b += strlen(b);
  782.                   if (*c != '\0' && *c != '&') {
  783.                     char* d = name[0];
  784.                     /* */
  785.                     while(*c != '\0' && *c != '&') {
  786.                       *d++ = *c++;
  787.                     }
  788.                     *d = '\0';
  789.                     d = unescape_http(catbuff,name[0]);
  790.                     if (d && *d) {
  791.                       strcpybuff(b, d);         /* value */
  792.                       b += strlen(b);
  793.                     } else {
  794.                       strcpybuff(b, name[3]);    /* empty replacement if any */
  795.                       b += strlen(b);
  796.                     }
  797.                   } else {
  798.                     strcpybuff(b, name[3]);    /* empty replacement if any */
  799.                     b += strlen(b);
  800.                   }
  801.                   strcpybuff(b, name[2]);    /* suffix */
  802.                   b += strlen(b);
  803.                 } else {
  804.                   strcpybuff(b, name[4]);    /* not found replacement if any */
  805.                   b += strlen(b);
  806.                 }
  807.                             } else {
  808.                                 strcpybuff(b, name[4]);    /* not found replacement if any */
  809.                                 b += strlen(b);
  810.                             }
  811.             }
  812.           break;
  813.           case '%': *b++='%'; break;
  814.           case 'n':    // nom sans ext
  815.             *b='\0';
  816.             if (dot_pos) {
  817.               if (!short_ver)    // Noms longs
  818.                 strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  819.               else
  820.                 strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  821.             } else {
  822.               if (!short_ver)    // Noms longs
  823.                 strcpybuff(b,nom_pos);
  824.               else
  825.                 strncatbuff(b,nom_pos,8);
  826.             }
  827.             b+=strlen(b);   // pointer α la fin
  828.             break;
  829.           case 'N':    // nom avec ext
  830.             // RECOPIE NOM + EXT
  831.             *b='\0';
  832.             if (dot_pos) {
  833.               if (!short_ver)    // Noms longs
  834.                 strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  835.               else
  836.                 strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  837.             } else {
  838.               if (!short_ver)    // Noms longs
  839.                 strcpybuff(b,nom_pos);
  840.               else
  841.                 strncatbuff(b,nom_pos,8);
  842.             }
  843.             b+=strlen(b);   // pointer α la fin
  844.             // RECOPIE NOM + EXT
  845.             *b='\0';
  846.             if (dot_pos) {
  847.               if (!short_ver)    // Noms longs
  848.                 strcpybuff(b,dot_pos+1);
  849.               else
  850.                 strncatbuff(b,dot_pos+1,3);
  851.             } else {
  852.               if (!short_ver)    // Noms longs
  853.                 strcpybuff(b,DEFAULT_EXT + 1);    // pas de..
  854.               else
  855.                 strcpybuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  856.             }
  857.             b+=strlen(b);   // pointer α la fin
  858.             //
  859.             break;
  860.           case 't':    // ext
  861.             *b='\0';
  862.             if (dot_pos) {
  863.               if (!short_ver)    // Noms longs
  864.                 strcpybuff(b,dot_pos+1);
  865.               else
  866.                 strncatbuff(b,dot_pos+1,3);
  867.             } else {
  868.               if (!short_ver)    // Noms longs
  869.                 strcpybuff(b,DEFAULT_EXT + 1);    // pas de..
  870.               else
  871.                 strcpybuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  872.             }
  873.             b+=strlen(b);   // pointer α la fin
  874.             break;
  875.           case 'p':    // path sans dernier /
  876.             *b='\0';
  877.             if (nom_pos != fil + 1) { // pas: /index.html (chemin nul)
  878.               if (!short_ver) {   // Noms longs
  879.                 strncatbuff(b,fil,(int) (nom_pos - fil) - 1);
  880.               } else {
  881.                 char BIGSTK pth[HTS_URLMAXSIZE*2],n83[HTS_URLMAXSIZE*2];
  882.                 pth[0]=n83[0]='\0';
  883.                 //
  884.                 strncatbuff(pth,fil,(int) (nom_pos - fil) - 1);
  885.                 long_to_83(opt->savename_83,n83,pth);
  886.                 strcpybuff(b,n83);
  887.               }
  888.             }
  889.             b+=strlen(b);   // pointer α la fin
  890.             break;
  891.           case 'h':    // host
  892.             *b='\0';
  893.             if (strcmp(adr_complete,"file://")==0) {
  894.               if (!short_ver)    // Noms longs
  895.                 strcpybuff(b,"localhost");
  896.               else
  897.                 strcpybuff(b,"local");
  898.             } else {
  899.               if (!short_ver)    // Noms longs
  900.                 strcpybuff(b,print_adr);
  901.               else
  902.                 strncatbuff(b,print_adr,8);
  903.             }
  904.             b+=strlen(b);   // pointer α la fin
  905.             break;
  906.           case 'M':         /* host/address?query MD5 (128-bits) */
  907.             *b='\0';
  908.             {
  909.               char digest[32+2];
  910.               char BIGSTK buff[HTS_URLMAXSIZE*2];
  911.               digest[0]=buff[0]='\0';
  912.               strcpybuff(buff,adr);
  913.               strcatbuff(buff,fil_complete);
  914.               domd5mem(buff,strlen(buff),digest,1);
  915.               strcpybuff(b,digest);
  916.             }
  917.             b+=strlen(b);   // pointer α la fin
  918.             break;
  919.           case 'Q': case 'q':         /* query MD5 (128-bits/16-bits) 
  920.                                          GENERATED ONLY IF query string exists! */
  921.                         {
  922.                             char md5[32 + 2];
  923.                             *b='\0';
  924.                             strncatbuff(b,url_md5(md5, fil_complete),(tok == 'Q')?32:4);
  925.                             b+=strlen(b);   // pointer α la fin
  926.                         }
  927.             break;
  928.           case 'r': case 'R':        // protocol
  929.             *b='\0';
  930.             strcatbuff(b, protocol_str[protocol]);
  931.             b+=strlen(b);   // pointer α la fin
  932.           break;
  933.  
  934.       /* Patch by Juan Fco Rodriguez to get the full query string */
  935.       case 'k':
  936.         {
  937.           char *d = strchr(fil_complete,'?');
  938.           if( d != NULL )
  939.           {
  940.             strcatbuff(b, d);
  941.             b+=strlen(b);
  942.           }
  943.         }
  944.         break;
  945.  
  946.         }
  947.       } else
  948.         *b++=*a++;
  949.     }
  950.     *b++='\0';
  951.     //
  952.     // Types prΘdΘfinis
  953.     //
  954.  
  955.   } 
  956.   //
  957.   // Structure originale
  958.   else if (opt->savename_type%100==0) { 
  959.     /* recopier www.. */
  960.     if (opt->savename_type!=100) {  
  961.       if (((opt->savename_type/1000)%2)==0) {    // >1000 signifie "pas de www/"
  962.         if (strcmp(adr_complete,"file://")==0) {
  963.           //## if (*adr==lOCAL_CHAR) {
  964.           if (opt->savename_83 != 1)  // noms longs
  965.             strcatbuff(save,"localhost");
  966.           else
  967.             strcatbuff(save,"local");
  968.         } else {
  969.           // adresse url
  970.           if (!opt->savename_83) {  // noms longs (et pas de .)
  971.             strcatbuff(save,print_adr);
  972.           } else {  // noms 8-3
  973.             if (strlen(print_adr)>4) {
  974.               if (strfield(print_adr,"www."))
  975.                 strncatbuff(save,print_adr+4,max_char);
  976.               else
  977.                 strncatbuff(save,print_adr,8);
  978.             } else strncatbuff(save,print_adr,max_char);
  979.           }
  980.         }
  981.         if (*fil!='/') strcatbuff(save,"/");
  982.       }
  983.     }
  984.   
  985.     hts_lowcase(save);
  986.         
  987.     /*
  988.     // ne sert α rien car a dΘja ΘtΘ filtrΘ normalement
  989.     if ((*fil=='.') && (*(fil+1)=='/'))          // ./index.html ** //
  990.       url_savename_addstr(save,fil+2);
  991.     else                                               // index.html ou /index.html
  992.       url_savename_addstr(save,fil);
  993.     if (save[strlen(save)-1]=='/') 
  994.       strcatbuff(save,DEFAULT_HTML);     // nommer page par dΘfaut!!
  995. */
  996.  
  997.     /* add name */
  998.     ADD_STANDARD_PATH;
  999.     ADD_STANDARD_NAME(0);
  1000.  
  1001.   }
  1002.   //
  1003.   // Structure html/image
  1004.   else {    
  1005.     // dossier "web" ou "www.xxx" ?
  1006.     if (((opt->savename_type/1000)%2)==0) {    // >1000 signifie "pas de www/"
  1007.       if ((opt->savename_type/100)%2) {
  1008.         if (strcmp(adr_complete,"file://")==0) {
  1009.         //## if (*adr==lOCAL_CHAR) {
  1010.           if (opt->savename_83 != 1)  // noms longs
  1011.             strcatbuff(save,"localhost/");
  1012.           else
  1013.             strcatbuff(save,"local/");
  1014.         } else {
  1015.           // adresse url
  1016.           if (!opt->savename_83) {  // noms longs
  1017.             strcatbuff(save,print_adr); strcatbuff(save,"/");
  1018.           } else {  // noms 8-3
  1019.             if (strlen(print_adr)>4) {
  1020.               if (strfield(print_adr,"www."))
  1021.                 strncatbuff(save,print_adr+4,max_char);
  1022.               else
  1023.                 strncatbuff(save,print_adr,max_char);
  1024.               strcatbuff(save,"/");
  1025.             } else { 
  1026.               strncatbuff(save,print_adr,max_char); strcatbuff(save,"/");
  1027.             }
  1028.           }
  1029.         }
  1030.       } else {
  1031.         strcatbuff(save,"web/");    // rΘpertoire gΘnΘral
  1032.       }
  1033.     } 
  1034.  
  1035.     // si un html α coup s√r
  1036.     if ( (ext_chg!=0) ? (ishtml_ext(ext) == 1) : (ishtml(opt,fil)==1) ) {
  1037.       if (opt->savename_type%100==2) {  // html/
  1038.         strcatbuff(save, "html/");
  1039.       }
  1040.     } else {
  1041.       if ((opt->savename_type%100==1) || (opt->savename_type%100==2)) {  // html & images
  1042.         strcatbuff(save, "images/");
  1043.       }
  1044.     }
  1045.     
  1046.     switch (opt->savename_type%100) {
  1047.     case 4: case 5: {           // sΘparer par types
  1048.       char* a=fil+strlen(fil)-1;
  1049.       // passer structures
  1050.       while(( a > fil) && (*a != '/') && (*a != '\\')) a--;      
  1051.       if ((*a=='/') || (*a=='\\')) a++;
  1052.  
  1053.       // html?
  1054.       if ( (ext_chg!=0) ? (ishtml_ext(ext)==1) : (ishtml(opt,fil)==1) ) {
  1055.         if (opt->savename_type%100==5)
  1056.           strcatbuff(save,"html/");
  1057.       } else {
  1058.         char* a=fil+strlen(fil)-1;
  1059.         while(( a> fil) && (*a != '/') && (*a != '.')) a--;      
  1060.         if (*a!='.')
  1061.           strcatbuff(save,"other");
  1062.         else
  1063.           strcatbuff(save,a+1);
  1064.         strcatbuff(save,"/");
  1065.       }
  1066.       /*strcatbuff(save,a);*/
  1067.       /* add name */
  1068.       ADD_STANDARD_NAME(0);
  1069.             }
  1070.       break;
  1071.     case 99: {                  // 'codΘ' .. c'est un gadget
  1072.       int i;
  1073.       int j;
  1074.       char* a;
  1075.       char C[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
  1076.       int L;
  1077.       // pseudo-CRC sur fil et adr pour initialiser gΘnΘrateur alΘatoire..
  1078.       unsigned int s=0;
  1079.       L = (int) strlen(C);
  1080.       for(i=0;i<(int) strlen(fil_complete);i++) {
  1081.         s+=(unsigned int) fil_complete[i];
  1082.       }
  1083.       for(i=0;i<(int) strlen(adr_complete);i++) {
  1084.         s+=(unsigned int) adr_complete[i];
  1085.       }
  1086.       srand(s);
  1087.       
  1088.       j = (int) strlen(save);
  1089.       for(i=0;i<8;i++) {
  1090.         char c=C[(rand()%L)];
  1091.         save[i+j]=c;
  1092.       }
  1093.       save[i+j]='\0';
  1094.       // ajouter extension
  1095.       a = fil + strlen(fil) - 1;
  1096.       while(( a > fil) && (*a != '/') && (*a != '.')) a--;
  1097.       if (*a=='.') {
  1098.         strcatbuff(save,a);    // ajouter
  1099.       }
  1100.              } 
  1101.       break;
  1102.     default: {   // noms sans les noms des rΘpertoires
  1103.       // ne garder que le nom, pas la structure
  1104.       /*
  1105.       char* a=fil+strlen(fil)-1;
  1106.       while(((int) a>(int) fil) && (*a != '/') && (*a != '\\')) a--;      
  1107.       if ((*a=='/') || (*a=='\\')) a++;
  1108.       strcatbuff(save,a);
  1109.       */
  1110.  
  1111.       /* add name */
  1112.       ADD_STANDARD_NAME(0);
  1113.             }
  1114.       break;
  1115.     }
  1116.  
  1117.     hts_lowcase(save);
  1118.  
  1119.     if (save[strlen(save)-1]=='/') 
  1120.       strcatbuff(save,DEFAULT_HTML);     // nommer page par dΘfaut!!
  1121.   }
  1122.  
  1123.  
  1124.   // vΘrifier qu'on ne doit pas forcer l'extension
  1125.   // par exemple, asp sera sauvΘ en html, cgi en html ou gif, xbm etc.. selon les cas
  1126.   /*if (ext_chg) {
  1127.     char* a=save+strlen(save)-1;
  1128.     while(((int) a>(int) save) && (*a!='.') && (*a!='/')) a--;
  1129.     if (*a=='.') *a='\0';  // couper
  1130.     // recopier extension
  1131.     strcatbuff(save,".");
  1132.     strcatbuff(save,ext);    // copier ext
  1133.   }*/
  1134.   // de mΩme en cas de manque d'extension on en place une de maniΦre forcΘe..
  1135.   // cela Θvite les /chez/toto et les /chez/toto/index.html incompatibles
  1136.   if (opt->savename_type != -1) {
  1137.     char* a=save+strlen(save)-1;
  1138.     while(( a > save) && (*a!='.') && (*a!='/')) a--;
  1139.     if (*a!='.') {   // agh pas de point
  1140.       //strcatbuff(save,".none");                 // a Θviter
  1141.       strcatbuff(save,".html");                   // prΘfΘrable!
  1142.       if ( (opt->debug>1) && (opt->log!=NULL) ) {
  1143.         HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Default HTML type set for %s%s"LF,adr_complete,fil_complete);
  1144.         test_flush;
  1145.       }
  1146.     }
  1147.   }
  1148.  
  1149.   // effacer pass au besoin pour les autentifications
  1150.   // (plus la peine : masquΘ au dΘbut)
  1151. /*
  1152.   {
  1153.     char* a=jump_identification(save);
  1154.     if (a!=save) {
  1155.       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1156.       char *b;
  1157.       tempo[0]='\0';
  1158.       strcpybuff(tempo,"[");
  1159.       b=strchr(save,':');
  1160.       if (!b) b=strchr(save,'@');
  1161.       if (b)
  1162.         strncatbuff(tempo,save,(int) b-(int) a);
  1163.       strcatbuff(tempo,"]");
  1164.       strcatbuff(tempo,a);
  1165.       strcpybuff(save,a);
  1166.     }
  1167.   }
  1168. */
  1169.  
  1170.   // Θviter les / au dΘbut (cause: N100)
  1171.   if (save[0]=='/') {
  1172.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1173.     strcpybuff(tempo,save+1);
  1174.     strcpybuff(save,tempo);
  1175.   }
  1176.  
  1177.   // changer les ~,:,",*,? en _ pour sauver sur disque
  1178.   hts_replace(save,'~','_');  // interdit sous unix (~foo)
  1179.   //
  1180.   hts_replace(save,'\\','_');
  1181.   hts_replace(save,':','_');  // interdit sous windows
  1182.   hts_replace(save,'*','_');  // interdit sous windows
  1183.   hts_replace(save,'?','_');  // doit pas arriver!!
  1184.   hts_replace(save,'\"','_');  // interdit sous windows
  1185.   hts_replace(save,'<','_');  // interdit sous windows
  1186.   hts_replace(save,'>','_');  // interdit sous windows
  1187.   hts_replace(save,'|','_');  // interdit sous windows
  1188.   //
  1189.   hts_replace(save,'@','_');
  1190.   if (opt->savename_83 == 2) { // CDROM
  1191.     // maybe other ones?
  1192.     hts_replace(save,'-','_');
  1193.     hts_replace(save,'=','_');
  1194.     hts_replace(save,'+','_');
  1195.   }
  1196.   //
  1197.   { // Θliminer les // (comme ftp://)
  1198.     char* a;
  1199.     while( (a=strstr(save,"//")) ) *a='_';
  1200.     // Eliminer chars spΘciaux
  1201.     a=save -1 ;
  1202.     while(*(++a))
  1203.       if ( ((unsigned char)(*a) <= 31)
  1204.           || ((unsigned char)(*a) == 127) )
  1205.         *a='_';
  1206.   }
  1207.  
  1208.    
  1209. #if HTS_OVERRIDE_DOS_FOLDERS
  1210.   /* Replace /foo/nul/bar by /foo/nul_/bar */
  1211.   {
  1212.     int i=0;
  1213.     while(hts_tbdev[i][0]) {
  1214.       char* a=save;
  1215.       while((a=strstrcase(a,(char*)hts_tbdev[i]))) {
  1216.         switch ( (int) a[strlen(hts_tbdev[i])] ) {
  1217.         case '\0':
  1218.         case '/':
  1219.         case '.': 
  1220.         {
  1221.           char BIGSTK tempo[HTS_URLMAXSIZE*2]; tempo[0]='\0';
  1222.           strncatbuff(tempo,save,(int) (a - save) + strlen(hts_tbdev[i]));
  1223.           strcatbuff(tempo,"_");
  1224.           strcatbuff(tempo,a+strlen(hts_tbdev[i]));
  1225.           strcpybuff(save,tempo);
  1226.                    }
  1227.           break;
  1228.         }
  1229.         a+=strlen(hts_tbdev[i]);
  1230.       }
  1231.       i++;
  1232.     }
  1233.   }
  1234.   /* Strip ending . or ' ' forbidden on windoz */
  1235.   {
  1236.     int len;
  1237.     char* a=save;
  1238.     while((a=strstr(a,"./"))) {
  1239.       *a = '_';
  1240.     }
  1241.     a=save;
  1242.     while((a=strstr(a," /"))) {
  1243.       *a = '_';
  1244.     }
  1245.     len = (int) strlen(save);
  1246.     if (len > 0 && ( save[len - 1] == '.' || save[len - 1] == ' ') ) {
  1247.       save[len - 1] = '_';
  1248.     }
  1249.   }
  1250. #endif
  1251.  
  1252.   // conversion 8-3 .. y compris pour les rΘpertoires
  1253.   if (opt->savename_83) {
  1254.     char BIGSTK n83[HTS_URLMAXSIZE*2];
  1255.     long_to_83(opt->savename_83,n83,save);
  1256.     strcpybuff(save,n83);
  1257.   }
  1258.  
  1259.   // enforce stricter ISO9660 compliance (bug reported by Steffo Carlsson)
  1260.   // Level 1 File names are restricted to 8 characters with a 3 character extension, 
  1261.   // upper case letters, numbers and underscore; maximum depth of directories is 8.
  1262.   // This will be our "DOS mode"
  1263.   // L2: 31 characters
  1264.   // A-Z,0-9,_
  1265.   if (opt->savename_83 > 0) {
  1266.     char *a, *last;
  1267.     for(last = save + strlen(save) - 1 ; last != save && *last != '/' && *last != '\\' && *last != '.' ; last--);
  1268.     if (*last != '.') {
  1269.       last = NULL;
  1270.     }
  1271.     for(a = save ; *a != '\0' ; a++) {
  1272.       if (*a >= 'a' && *a <= 'z') {
  1273.         *a -= 'a' - 'A';
  1274.       }
  1275.       else if (*a == '.') {
  1276.         if (a != last) {
  1277.           *a = '_';
  1278.         }
  1279.       }
  1280.       else if ( ! ( (*a >= 'A' && *a <= 'Z') || (*a >= '0' && *a <= '9') || *a == '_' || *a == '/' || *a == '\\') ) {
  1281.         *a = '_';
  1282.       }
  1283.     }
  1284.   }
  1285.  
  1286.   /* ensure that there is no ../ (potential vulnerability) */
  1287.   fil_simplifie(save);
  1288.  
  1289.     /* callback */
  1290.   RUN_CALLBACK5(opt, savename, adr_complete,fil_complete,referer_adr,referer_fil,save);
  1291.  
  1292.   if ( (opt->debug>0) && (opt->log!=NULL) ) {
  1293.         HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: save-name: local name: %s%s -> %s"LF,adr,fil,save);
  1294.         test_flush;
  1295.     }
  1296.  
  1297.     /* Ensure that the MANDATORY "temporary" extension is set */
  1298.     if (ext_chg_delayed) {
  1299.         char *ptr;
  1300.         char *lastDot = NULL;
  1301.         for(ptr = save ; *ptr != 0 ; ptr++) {
  1302.             if (*ptr == '.') {
  1303.                 lastDot = ptr;
  1304.             } else if (*ptr == '/' || *ptr == '\\') {
  1305.                 lastDot = NULL;
  1306.             }
  1307.         }
  1308.         if (lastDot == NULL) {
  1309.             strcatbuff(save, "." DELAYED_EXT);
  1310.         } else if (!IS_DELAYED_EXT(save)) {
  1311.             strcatbuff(lastDot, "." DELAYED_EXT);
  1312.         }
  1313.     }
  1314.  
  1315.   // chemin primaire Θventuel A METTRE AVANT
  1316.   if (strnotempty(StringBuff(opt->path_html))) {
  1317.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1318.     strcpybuff(tempo,StringBuff(opt->path_html));
  1319.     strcatbuff(tempo,save);
  1320.     strcpybuff(save,tempo);
  1321.   }
  1322.  
  1323.  
  1324.   // vΘrifier que le nom n'est pas dΘja pris...
  1325.   if (liens!=NULL) { 
  1326.     int nom_ok;
  1327.     do {
  1328.       int i;
  1329.       //
  1330.       nom_ok=1;  // α priori bon
  1331.       // on part de la fin pour optimiser, plus les opti de taille pour aller encore plus vite..
  1332. #if DEBUG_SAVENAME
  1333. printf("\nStart search\n");
  1334. #endif
  1335.  
  1336.       i=hash_read(hash,save,"",0,0);      // lecture type 0 (sav)
  1337.       if (i>=0)
  1338.           {
  1339.             int sameAdr = ( strfield2(liens[i]->adr, normadr) != 0 );
  1340.             int sameFil;
  1341.                         // NO - URL hack is only for stripping // and www.
  1342.             //if (opt->urlhack != 0)
  1343.             //  sameFil = ( strfield2(liens[i]->fil, normfil) != 0);
  1344.             //else
  1345.             sameFil = ( strcmp(liens[i]->fil, normfil) == 0);
  1346.             if (sameAdr && sameFil)
  1347.             {    // ok c'est le mΩme lien, adresse dΘja dΘfinie
  1348.               /* Take the existing name not to screw up with cAsE sEnSiTiViTy of Linux/Unix */
  1349.               if (strcmp(liens[i]->sav, save) != 0) {
  1350.                 strcpybuff(save, liens[i]->sav);
  1351.               }
  1352.               i=0;
  1353. #if DEBUG_SAVENAME
  1354. printf("\nOK ALREADY DEFINED\n",13,i);
  1355. #endif
  1356.             } else {  // utilisΘ par un AUTRE, changer de nom
  1357.               char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1358.               char* a=save+strlen(save)-1;
  1359.               char* b;
  1360.               int n=2;       
  1361.               char collisionSeparator = ( (opt->savename_83 != 2) ? '-' : '_');
  1362.               tempo[0]='\0';
  1363.  
  1364. #if DEBUG_SAVENAME
  1365. printf("\nWRONG CASE UNMATCH : \n%s\n%s, REDEFINE\n",liens[i]->fil,fil_complete);
  1366. #endif
  1367.               nom_ok=0;
  1368.               i=0;
  1369.               
  1370.               while(( a > save) && (*a!='.') && (*a!='\\') && (*a!='/')) a--;
  1371.               if (*a=='.')
  1372.                 strncatbuff(tempo,save,(int) (a - save));
  1373.               else
  1374.                 strcatbuff(tempo,save);
  1375.               
  1376.               // tester la prΘsence d'un -xx (ex: index-2.html -> index-3.html)
  1377.               b=tempo+strlen(tempo)-1;
  1378.               while (isdigit((unsigned char)*b)) b--;
  1379.               if (*b == collisionSeparator) {
  1380.                 sscanf(b+1,"%d",&n);
  1381.                 *b='\0';    // couper
  1382.                 n++;  // plus un
  1383.               }
  1384.               
  1385.               // en plus il faut gΘrer le 8-3 .. pas facile le client
  1386.               if (opt->savename_83) {
  1387.                 int max;
  1388.                 char* a=tempo+strlen(tempo)-1;
  1389.                 while(( a > tempo) && (*a!='/')) a--;
  1390.                 if (*a=='/') a++;
  1391.                 max=max_char-1-nombre_digit(n);
  1392.                 if ((int) strlen(a)>max)
  1393.                   *(a+max)='\0';  // couper sinon il n'y aura pas la place!
  1394.               }
  1395.               
  1396.               // ajouter -xx (ex: index.html -> index-2.html)
  1397.               sprintf(tempo+strlen(tempo),"%c%d", collisionSeparator, n);
  1398.               
  1399.               // ajouter extension
  1400.               if (*a=='.')
  1401.                 strcatbuff(tempo,a);
  1402.               
  1403.               strcpybuff(save,tempo);
  1404.               
  1405.               //printf("switched: %s\n",save);
  1406.               
  1407.             }  // if
  1408.           }
  1409. #if DEBUG_SAVENAME
  1410. printf("\nEnd search, %s\n",fil_complete);
  1411. #endif
  1412.     } while(!nom_ok);
  1413.     
  1414.   }
  1415.   
  1416.   //printf("'%s' %s %s\n",save,adr,fil);
  1417.       
  1418.   return 0;
  1419. }
  1420.  
  1421. /* nom avec md5 urilisΘ partout */
  1422. void standard_name(char* b,char* dot_pos,char* nom_pos,char* fil_complete,int short_ver) {
  1423.     char md5[32 + 2];
  1424.  
  1425.   b[0]='\0';
  1426.   /* Nom */
  1427.   if (dot_pos) {
  1428.     if (!short_ver)    // Noms longs
  1429.       strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  1430.     else
  1431.       strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  1432.   } else {
  1433.     if (!short_ver)    // Noms longs
  1434.       strcatbuff(b,nom_pos);
  1435.     else
  1436.       strncatbuff(b,nom_pos,8);
  1437.   }
  1438.   /* MD5 - 16 bits */
  1439.   strncatbuff(b,url_md5(md5,fil_complete),4);
  1440.   /* Ext */
  1441.   if (dot_pos) {
  1442.     strcatbuff(b,".");
  1443.     if (!short_ver)    // Noms longs
  1444.       strcatbuff(b,dot_pos+1);
  1445.     else
  1446.       strncatbuff(b,dot_pos+1,3);
  1447.   } else {
  1448.     if (!short_ver)    // Noms longs
  1449.       strcatbuff(b,DEFAULT_EXT + 1);    // pas de..
  1450.     else
  1451.       strcatbuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  1452.   }
  1453. }
  1454.  
  1455.  
  1456. /* Petit md5 */
  1457. char* url_md5(char* digest, char* fil_complete) {
  1458.   char* a;
  1459.   digest[0]='\0';
  1460.   a=strchr(fil_complete,'?');
  1461.   if (a) {
  1462.     if (strlen(a)) {
  1463.       char BIGSTK buff[HTS_URLMAXSIZE*2];
  1464.       a++;
  1465.       digest[0]=buff[0]='\0';
  1466.       strcatbuff(buff,a);         /* query string MD5 */
  1467.       domd5mem(buff,strlen(buff),digest,1);
  1468.     }
  1469.   }
  1470.   return digest;
  1471. }
  1472.  
  1473. // interne α url_savename: ajoute une chaεne α une autre avec \ -> /
  1474. void url_savename_addstr(char* d,char* s) {
  1475.   int i = (int) strlen(d);
  1476.   while(*s) {
  1477.     if (*s=='\\')  // remplacer \ par des /
  1478.       d[i++]='/';
  1479.     else
  1480.       d[i++]=*s;
  1481.     s++;
  1482.   }
  1483.   d[i]='\0';
  1484. }
  1485.  
  1486. #undef test_flush
  1487.